<?php

namespace app\api\controller;

use app\api\model\GoodsModel;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\MissException;
use app\lib\exception\ParameterException;
use think\Controller;

class Goods extends Controller
{
    /**
     * 商品列表
     */
    public function goodsList()
    {
        $list = GoodsModel::all();
        foreach ($list as $v){
            $overtime = strtotime($v['overtime']);
            $time = $overtime-time();
            $v['description']=explode(',',$v['description']);
            if ($time>0){
                $v['overtime'] = timetostr($time);
            }else {
                $v['goods_new_price'] = '';
                $v['overtime'] = '0-0-0';
            }
        }
        return json(msg(1,$list,'获取数据成功'));
    }

    /**
     * 商品详情.
     */
    public function goodsDetail()
    {
        $id = input('param.id');
        $goods = GoodsModel::get($id);
        $checkId = new IDMustBePositiveInt();
        $checkId->goCheck();
        if (!$goods){
            throw new MissException(['msg'=>'请求的商品不存在']);
        }
        $overtime = strtotime($goods['overtime']);
        $time = $overtime-time();
        if ($time>0){
            $goods['overtime'] = timetostr($time);
        }else {
            $goods['goods_new_price'] = '';
            $goods['overtime'] = '0-0-0';
        }
        return json(msg(1,$goods,'查询成功'));
    }

}
